package com.example.sixth.coree.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.sixth.coree.entity.Userinfo;
import com.example.sixth.coree.service.UserinfoService;
import com.example.sixth.coree.utils.MiscUtil;
import com.example.sixth.coree.entity.ReturnData;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Userinfo)表控制层
 *
 * @author Zixiang Gao
 * @since 2021-06-23 18:15:15
 */
@RestController
@CrossOrigin
@RequestMapping("api/user")
public class UserinfoController {
    /**
     * 服务对象
     */

    @Autowired
     private RedisTemplate redisTemplate;
    @Autowired
    private UserinfoService service;

    public Userinfo getUserbyemail(String email){
        Userinfo userinfo  = service.queryByAccount(email);
        return userinfo;
    }
    @RequestMapping("/getmyinfo")
    public ReturnData getUserinfoService(@RequestBody Map request) {
        String access_token = (String) request.get("access_token");
        String plat = (String) request.get("plat");
        Integer version = (Integer) request.get("version");
        if(access_token.equals("65536")){
            Map<String, Object> map = new HashMap();
            map.put("user_id",-1);
            map.put("user_name","Ghost");
            map.put("user_head","new/images/guest.png");
            return new ReturnData(200,"",map);
        }

        Map<String, Object> map = new HashMap();

        String s = MiscUtil.verifyToken(access_token);
        if(s==null)  return new ReturnData(401,"登录过期，请重新登录",map);
        else{

            return new ReturnData(200,"",service.queryById(Integer.parseInt(s)));
        }
    }
    public boolean isnull(String str){
        if(str == null || "".equals(str)){
            return true;
        }
        return false;
    }
    public boolean judge(String user,String password){
        if(isnull(user)||isnull(password)){
            return false;
        }

        return judgebymysql(user, password)||judgebyredis(user,password);
    }
    public boolean judgebymysql(String user,String password){

        Userinfo userinfo = service.queryByBoth(user,password);
        return userinfo!=null;
    }
    public boolean judgebyredis(String user,String password){
        String v_code = (String) redisTemplate.opsForValue().get(user);
        if(isnull(v_code)){
            return false;
        }
        return Objects.equals(v_code, password);
    }
    public boolean register(String user,String password){
        Userinfo userinfo = new Userinfo();
        userinfo.setUser_account(user);
        userinfo.setUser_passwd(password);
        userinfo.setUser_name("用户"+ RandomUtil.randomString(4));
        userinfo.setUser_sex(0);
        userinfo.setUser_head("new/images/nohead.png");
        return service.insert(userinfo);
    }
    //{"code":500,"msg":"\u5e10\u53f7\u6216\u5bc6\u7801\u9519\u8bef"}
    //{"code":200,"msg":"\u767b\u5f55\u6210\u529f","data":{"access_token":"deb8c8cbf92a2a772dc847cf4c11ca7bfeed9c4199999deb8c8cbf92a2a772dc847cf4c11ca7bfeed9c41"}}
    @RequestMapping("login")
    public ReturnData login(@RequestBody Userinfo userinfo){
        String user_account = userinfo.getUser_account();
        String user_password = userinfo.getUser_passwd();
        if(judge(user_account,user_password)){
            Userinfo userbyemail = getUserbyemail(user_account);
            if(userbyemail==null){
                register(user_account,user_password);
            }
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("access_token", MiscUtil.createToken(userbyemail.getUser_id().toString()));
            return new ReturnData(200,"登陆成功",objectObjectHashMap);
        }
        return new ReturnData(500,"账号密码有误",null);
    }
    @RequestMapping("updateMyInfo")
    public ReturnData updateMyInfo(@RequestBody Map<String, String> mp){
        String access_token = mp.get("access_token");
        if(MiscUtil.verifyToken(access_token)==null){
            return new ReturnData(401,"无权修改",null);
        }
        Userinfo userinfo=new Userinfo();
        userinfo.setUser_id(Integer.parseInt(mp.get("user_id")));
        userinfo.setUser_head(mp.get("user_head"));
        userinfo.setUser_name(mp.get("user_name"));
        userinfo.setUser_passwd(mp.get("user_password"));
        userinfo.setUser_sex(Integer.parseInt(mp.get("user_sex")));
        userinfo.setUser_remark(mp.get("user_remark"));
        userinfo.setUser_touchtip(mp.get("user_touchtip"));

        if(service.update(userinfo)==false){
            return new ReturnData(401,"修改失败",null);
        }
        return new ReturnData(200,"修改成功",null);
    }
    boolean updateUser(String id,String user_head,String name,String user_passwd,String user_sex,String user_remark,String user_touchtip){
        Userinfo userinfo = new Userinfo();
        userinfo.setUser_id(Integer.parseInt(id));
        userinfo.setUser_head(user_head);
        userinfo.setUser_name(name);
        userinfo.setUser_passwd(user_passwd);
        userinfo.setUser_sex(Integer.parseInt(user_sex));
        userinfo.setUser_remark(user_remark);
        userinfo.setUser_touchtip(user_touchtip);

      return service.update(userinfo);
    }
    @RequestMapping("getuserinfo")
    ReturnData getUserInfo(@RequestBody Map mp){
        Integer user_id = Integer.parseInt(mp.get("user_id").toString());
        return new ReturnData(200,"查询成功",service.queryById(user_id));
    }

}